//
//  main.m
//  NSDictionary
//
//  Created by wenjim on 17/10/24.
//  Copyright © 2017年 WenJim. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    
    
    // 1.如何创建
    NSDictionary * dic = [NSDictionary dictionaryWithObject:@"cwj" forKey:@"name"];
    // 根据Key获取字典里的内容
    NSString * name = [dic objectForKey:@"name"];
    NSLog(@"name = %@",name);
    
    
    // 注意：key 和 Value 是一一对应的
    /*
    NSDictionary * dict = [NSDictionary dictionaryWithObjects:@[@"cwj",@"26",@"1.72"] forKeys:@[@"name",@"age",@"height"]];
    NSLog(@"name = %@,age = %@,height = %@",[dict objectForKey:@"name"],[dict objectForKey:@"age"],[dict objectForKey:@"height"]);
    NSLog(@"dict = %@",dict);
    */
    
//    NSDictionary * dict = @{key:value};
    /*
    NSDictionary * dict = @{@"name":@"cwj"};
    NSLog(@"name = %@",dict[@"name"]);
                            
    NSDictionary * dict2 = @{@"name":@"cwj",
                            @"age":@"26",
                            @"height":@"1.72"};
    NSLog(@"name = %@,age = %@,height = %@",dict2[@"name"],dict2[@"age"],dict2[@"height"]);
    */
    
    // 2.字典的遍历
    NSDictionary * dict2 = @{@"name":@"cwj",
                             @"age":@"26",
                             @"height":@"1.72"};
    // 2.1 如何获取字典中 key 和 value 的个数，在字典中 key 称之为 键， value 称之为 值
    
    NSLog(@"count = %lu",[dict2 count]);
    
    /*
    for (int i = 0; i < dict2.count; i ++) {
        // 获取字典中所有的key
        NSArray * keyArr = [dict2 allKeys];
        // 取出当前位置对应的key
//        NSLog(@"key = %@",keyArr[i]);
        NSString * key = keyArr[i];
        NSString * value = dict2[key];
        NSLog(@"key = %@,value = %@",key,value);
    }
    */
    
    /*
    // 如果通过forin 遍历字典，会将所有的 key 赋值给前面的obj
    for (NSString * key in dict2) {
//        NSLog(@"key = %@",key);
        NSString * value = dict2[key];
        NSLog(@"key = %@,value = %@",key,value);
    }
     */
    
    [dict2 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"key = %@,value = %@",key,obj);
    }];
    
    
    // 3. 字典文件读写
    NSDictionary * dict3 = @{@"name":@"cwj",
                             @"age":@"26",
                             @"height":@"1.72"};
    
    BOOL flag = [dict3 writeToFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/demo文件保存/NSDictionary.plist" atomically:YES];
    if (flag) {
        NSLog(@"写入成功");
    }else{
        NSLog(@"写入失败");
    }
    
    // 注意：字典和数组不同，字典中保存的数据是无序的
    NSDictionary * newDic = [NSDictionary dictionaryWithContentsOfFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/demo文件保存/NSDictionary.plist"];
    NSLog(@"newDic = %@",newDic);
    
    NSArray * arr = @[@10,@20,@5,@30];
    [arr writeToFile:@"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/demo文件保存/array.plist" atomically:YES];
    
    return 0;
}
